// pages/login/login.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone:'15179716321',
    password:'moran123'
  },

  // 收集输入框数据的函数
  handleInput(event){
    const type = event.currentTarget.dataset.type
    this.setData({
      [type]:event.detail.value
    })
  },
  async login(){
    let result
    const {phone,password} = this.data
    if(!phone){
      wx.showToast({
        title:'手机号不能为空',
        icon:'none'
      })
      return
    }
    if(!/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone)){
      wx.showToast({
        title:'手机号格式错误',
        icon:'none'
      })
      return
    }
    if(!password){
      wx.showToast({
        title:'密码不能为空',
        icon:'none'
      })
      return
    }
    // 登录
    if(phone === '15179716321' && password === "moran123"){
      result = {
        code: 200,
        profile: {
          accountStatus: 0,
          authStatus: 0,
          authority: 0,
          avatarDetail: null,
          avatarImgId: 1410673435451141,
          avatarImgIdStr: "1410673435451141",
          avatarUrl: "https://p1.music.126.net/sfC0WIwWh7hKLRJ8qkLqYQ==/1410673435451141.jpg",
          backgroundImgId: 109951163518363380,
          backgroundImgIdStr: "109951163518363378",
          backgroundUrl: "https://p1.music.126.net/WPwET_qY0OnSqKL_3v90Yw==/109951163518363378.jpg",
          birthday: 863884800000,
          city: 360700,
          defaultAvatar: false,
          description: "",
          detailDescription: "",
          djStatus: 0,
          eventCount: 3,
          expertTags: null,
          experts: {},
          followed: false,
          followeds: 7,
          follows: 17,
          gender: 1,
          mutual: false,
          nickname: "好爸爸和狗时时",
          playlistBeSubscribedCount: 2,
          playlistCount: 10,
          province: 360000,
          remarkName: null,
          signature: "",
          userId: 75543868,
          userType: 0,
          vipType: 0 ,
          token: "465a9335a4fc540442fdacc247f73293c29bb3340d73fa4a2f55203ad13740491e8907c67206e1edae1bdda0473178eb9a9dbd7776ae3e563aa0f1590ccac8b21c2e9e0a195af7391b93ac14e0ed86ab"}
      }
    }else  if (phone==="15179716321" && password!=="moran123"){
      result={
        code:502,
        message:"密码错误"
      }
    }
    const code = result.code;
    const fun = function () {
      wx.showToast({
        title: '密码不正确请重新输入',
        icon: 'none'
      })
      return
    }
    // 策略模式
    const codeFn = {
      200(){
        wx.showToast({
          title: '登录成功',
          icon: 'none'
        })
        wx.switchTab({
          url: "/pages/personal/personal"
        })
        // 保存数据
        wx.setStorage({
          key:'userInfo',
          data: result.profile
        })
        wx.setStorageSync('cookie',"MUSIC_U=465a9335a4fc540442fdacc247f73293c29bb3340d73fa4a32256302e2dc8d131e8907c67206e1edc3d6d1f85e05ff9b0b504180123d9d923aa0f1590ccac8b21c2e9e0a195af7391b93ac14e0ed86ab; Max-Age=1296000; Expires=Thu, 08 Sep 2022 07:52:43 GMT; Path=/; Domain=.music.163.com; HTTPOnly")
        return
      },
      400(){
        wx.showToast({
          title: '账号格式错误',
          icon: 'none'
        })
        return
      },
      501(){
        wx.showToast({
          title: '账号不存在',
          icon: 'none'
        })
        return
      },
      502: fun,
      503: fun
    }
    codeFn[code] && codeFn[code]()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

})